package com.watson.todolist.testing;

import com.watson.todolist.constants.DatabaseConstants;
import com.watson.todolist.database.AccountManager;
import com.watson.todolist.database.DatabaseManager;
import com.watson.todolist.database.GenericEncryption;
import com.watson.todolist.database.ToDoListManager;
import com.watson.todolist.to.TodoItem;

import junit.framework.Assert;
import android.accounts.Account;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

public class DonnieTest1 extends AndroidTestCase{
	
		public void testTodoItemCompleted() throws Throwable {
			//Open database for manipulation
			DatabaseManager dbManager = new DatabaseManager();
			SQLiteDatabase db = dbManager.open(getContext());

			//Access account manager and create a user
			AccountManager accountManager = new AccountManager(db);
			long userId = accountManager.createUserAccount("JohnDoe", "JohnDoe@JohnDoe.com", "JohnDoe");
			
			//Access TodoListManager and create incomplete todo item
			ToDoListManager todoListManager = new ToDoListManager(db);
			long todoId = todoListManager.createTodoListItem(userId, "JohnDoe Item Name", 0, 0, 0, 0, "JohnDoe Item Description", 0, 0, 0);
					
			//Check to see if todo item is incomplete
			Assert.assertTrue(!todoListManager.isChecked(todoId));
			
			//Get todo item before update
			TodoItem todoItem1 = todoListManager.getTodoListItemByTodoId(todoId);
			
			//update the todo item to complete
			todoListManager.updateTodoListItemByTodoItemId(todoItem1.getTodoItemId(),
					todoItem1.getUserId(),todoItem1.getTodoItemName(),
					todoItem1.getTodoItemCategoryCode(), todoItem1.getTodoTime(),
					todoItem1.getLatitude(), todoItem1.getLongitude(),
					todoItem1.getDescription(), todoItem1.getAlarmRecurrenceType(),
					todoItem1.getPriorAlarmReminderTime(), 1);
			
			//Check to see if todo item is complete
			Assert.assertTrue(todoListManager.isChecked(todoId));
			
			//update the todo item to incomplete
			todoListManager.updateTodoListItemByTodoItemId(todoItem1.getTodoItemId(),
					todoItem1.getUserId(),todoItem1.getTodoItemName(),
					todoItem1.getTodoItemCategoryCode(), todoItem1.getTodoTime(),
					todoItem1.getLatitude(), todoItem1.getLongitude(),
					todoItem1.getDescription(), todoItem1.getAlarmRecurrenceType(),
					todoItem1.getPriorAlarmReminderTime(), 0);
			
			//Check to see if todo item is incomplete
			Assert.assertTrue(!todoListManager.isChecked(todoId));
			
			//Close database
			dbManager.close();
	    }

	    public void testAccountCreationByUserEmail() throws Throwable {
	    	//Open database for manipulation
			DatabaseManager dbManager = new DatabaseManager();
			SQLiteDatabase db = dbManager.open(getContext());

			//Access account manager
			AccountManager accountManager = new AccountManager(db);
			
			//Create user account with user name kim
			accountManager.createUserAccount("Kim", "kim@kim.com", "kimpass");
			
			//Create user account with user name asana
			accountManager.createUserAccount("Asana", "test@test.com", "asana");
			
			//Create user account with user name abby
			accountManager.createUserAccount("AbbyJohnson", "ab@johnson.com", "123pass");
			
			//Create user account with null values
			long errorId = accountManager.createUserAccount(null, null, null);
			Assert.assertEquals(-1, errorId);
			
			//Get accounts by email
			Cursor cursorKim = accountManager.getUserAccountByUserEmail("kim@kim.com");
			Cursor cursorAsana = accountManager.getUserAccountByUserEmail("test@test.com");
			Cursor cursorAbby = accountManager.getUserAccountByUserEmail("ab@johnson.com");
			
			//Test user names
			Assert.assertEquals("Kim", cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			Assert.assertEquals("Asana", cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			Assert.assertEquals("AbbyJohnson", cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			
			//Test emails
			Assert.assertEquals("kim@kim.com", cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			Assert.assertEquals("test@test.com", cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			Assert.assertEquals("ab@johnson.com", cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			
			//Ecrypt passwords
			String kimEncrypted = GenericEncryption.encrypt("kimpass");
			String asanaEncrypted = GenericEncryption.encrypt("asana");
			String AbbyEncrypted = GenericEncryption.encrypt("123pass");
			
			//Test passwords
			Assert.assertEquals(kimEncrypted, cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
			Assert.assertEquals(asanaEncrypted, cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
			Assert.assertEquals(AbbyEncrypted, cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
	    }
	    
	    public void testAccountCreationByUserName() throws Throwable {
	    	//Open database for manipulation
			DatabaseManager dbManager = new DatabaseManager();
			SQLiteDatabase db = dbManager.open(getContext());

			//Access account manager
			AccountManager accountManager = new AccountManager(db);
			
			//Create user account with user name kim
			accountManager.createUserAccount("Kim", "kim@kim.com", "kimpass");
			
			//Create user account with user name asana
			accountManager.createUserAccount("Asana", "test@test.com", "asana");
			
			//Create user account with user name abby
			accountManager.createUserAccount("AbbyJohnson", "ab@johnson.com", "123pass");
			
			//Get accounts by email
			Cursor cursorKim = accountManager.getUserAccountByUserName("Kim");
			Cursor cursorAsana = accountManager.getUserAccountByUserName("Asana");
			Cursor cursorAbby = accountManager.getUserAccountByUserName("AbbyJohnson");
			
			//Test user names
			Assert.assertEquals("Kim", cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			Assert.assertEquals("Asana", cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			Assert.assertEquals("AbbyJohnson", cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_USER_NAME)));
			
			//Test emails
			Assert.assertEquals("kim@kim.com", cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			Assert.assertEquals("test@test.com", cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			Assert.assertEquals("ab@johnson.com", cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_EMAIL_ADDRESS)));
			
			//Ecrypt passwords
			String kimEncrypted = GenericEncryption.encrypt("kimpass");
			String asanaEncrypted = GenericEncryption.encrypt("asana");
			String AbbyEncrypted = GenericEncryption.encrypt("123pass");
			
			//Test passwords
			Assert.assertEquals(kimEncrypted, cursorKim.getString(cursorKim.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
			Assert.assertEquals(asanaEncrypted, cursorAsana.getString(cursorAsana.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
			Assert.assertEquals(AbbyEncrypted, cursorAbby.getString(cursorAbby.getColumnIndex(DatabaseConstants.ACCOUNT_TABLE_PASSWORD)));
	    }
	    
	    
	    public void testPasswordEncryption() throws Throwable {			
			//Ecrypt passwords
			String ecryptedPass1 = GenericEncryption.encrypt("newyears2008");
			String ecryptedPass2 = GenericEncryption.encrypt("androidtest");
			String ecryptedPass3 = GenericEncryption.encrypt("passwordchecker");
			
			//Test ecrypted passwords
			Assert.assertEquals(ecryptedPass1, "7KkHzzdPgIYioXTICMvVNw==\n");
			Assert.assertEquals(ecryptedPass2, "jUSslTFXs5gKJ41FBMBQnA==\n");
			Assert.assertEquals(ecryptedPass3, "x1630oRyAeTzBuLcX39wtg==\n");
	    }
}
